﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common ;
using System.Data.OracleClient;

namespace DbSharp.Tools.DbConnectionBuilder.Core.Plugin
{
    class OracleConnectionPlugin : IDbConnectionPlugin
    {

        #region IDbConnectionPlugin Members

        public PluginNamePair PluginNamePair
        {
            get { return new PluginNamePair("System.Data.OracleClient") { DisplayName = "Oracle Driver" }; }
        }

        public DbConnectionStringBuilder CreateConnectionStrBuilder(DbServerCredential credential)
        {
            //Data Source=sprs;User ID=user_obj;Password=1234;Unicode=True
            var builder = new OracleConnectionStringBuilder();
            builder.DataSource = credential.DataSource;
            builder.UserID = credential.UserId;
            builder.Password = credential.Password;
            builder.IntegratedSecurity = false;
            builder.Unicode = true;

            //ORA-2041 usually occurs when you're fetching across a database link (Oracle 8i) without a distributed transaction. 
            using (OracleConnection cnn = new OracleConnection(builder.ConnectionString))
            {
                try
                {
                    cnn.Open();
                    int index = cnn.ServerVersion.IndexOf(" 8.", StringComparison.Ordinal);
                    if (index > -1)
                    {
                        //add addtional property for oracle 8i to prevent ORA-2041 error
                        builder.OmitOracleConnectionName = true;
                    }
                }
                catch
                { 
                    //catch all kinds of exception
                }
            }

            return builder;
        }

        public DbServerCredential CreateCredential(string connectionString)
        {
            DbServerCredential credential = new DbServerCredential();
            credential.UserIdEnabled = true;
            credential.PasswordEnabled = true;
            credential.DataSourceEnabled = true;

            var builder = new OracleConnectionStringBuilder(connectionString);
            object userId;
            if (builder.TryGetValue("User ID", out userId))
            {
                credential.UserId = userId.ToString();
            }
            object pwd;
            if (builder.TryGetValue("Password", out pwd))
            {
                credential.Password = pwd.ToString();
            }
            object dataSource;
            if (builder.TryGetValue("Data Source", out dataSource))
            {
                credential.DataSource = dataSource.ToString();
            }

            credential.AdditionalInfo = "This driver is deprecated. Please use Oracle Devart Driver instead.";
            
            return credential;
        }

        public DbConnection CreateDbConnection(string connectionString)
        {
            return new OracleConnection(connectionString);
        }

        public List<FileVersionPair> GetCandidateDbFileVersionPairs()
        {
            var list = new List<FileVersionPair>();
            return list;
        }


        #endregion
    }
}
